#include <QString>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QEventLoop>
#include <QNetworkReply>
#include <QJsonParseError>
#include <QJsonObject>
#include <QUrlQuery>
#include <QCoreApplication>

int NormalPostRequest(const QString& paramer1, const QString& paramer2)
{
  //生成对应的网址请求
  QNetworkRequest request;
  QString scheme = "http";
  QString serverAddr = "192.168.0.1";
  QString port = "80";
  QString requestHeader = scheme + QString("://") + serverAddr + QString(":") + port;
  QString fullRequest = requestHeader + "/api/v1/user/postrequest";
  request.setUrl(QUrl(fullRequest));
  request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

  //获取对应的参数数据
  QByteArray data;
  data.append("paramer1=");
  data.append(paramer1.toUtf8());
  data.append("&paramer2=");
  data.append(paramer2.toUtf8());

  //发送请求
  QNetworkAccessManager manager;
  QNetworkReply* reply = manager.post(request, data);
  QEventLoop eventLoop;
  QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
  eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
  if (reply->error() != QNetworkReply::NoError)
  {
    return reply->error();
  }

  //解析返回的Json结果
  QByteArray replyData = reply->readAll();
  QJsonParseError json_error;
  QJsonDocument jsonDoc(QJsonDocument::fromJson(replyData, &json_error));
  if (json_error.error != QJsonParseError::NoError)
  {
    return -1;
  }
  QJsonObject rootObj = jsonDoc.object();
  QString codeStr = rootObj.value("code").toString();
  if (codeStr.compare("200") == 0)
  {
    //返回代码为200的时候证明请求成功对包含的结构数据进行处理
    if (rootObj.contains("result"))
    {
    }
    return 0;
  }
  else
  {
    //请求失败对对应的处理
    return codeStr.toInt();
  }
}

int LoginRequest(const QString& paramer1, const QString& paramer2)
{
  QString ipPort = "http://47.99.74.239:7000";
  QString loginApi = "/user/login";
  QUrl url(ipPort + loginApi);
  QUrlQuery query;

  query.addQueryItem("username", "test");
  query.addQueryItem("password", "test");

  url.setQuery(query.query());

  QNetworkRequest request(url);
  request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

  //发送请求
  QNetworkAccessManager manager;
  QNetworkReply* reply = manager.post(request, "");

  QEventLoop eventLoop;
  QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
  eventLoop.exec(QEventLoop::ExcludeUserInputEvents);

  if (reply->error() != QNetworkReply::NoError)
  {
    return reply->error();
  }

  //解析返回的Json结果
  QByteArray replyData = reply->readAll();
  qDebug() << replyData;
  QJsonParseError json_error;
  QJsonDocument jsonDoc(QJsonDocument::fromJson(replyData, &json_error));
  if (json_error.error != QJsonParseError::NoError)
  {
    return -1;
  }
  QJsonObject rootObj = jsonDoc.object();
  QString codeStr = rootObj.value("code").toString();
  if (codeStr.compare("200") == 0)
  {
    //返回代码为200的时候证明请求成功对包含的结构数据进行处理
    if (rootObj.contains("result"))
    {
    }
    return 0;
  }
  else
  {
    //请求失败对对应的处理
    return codeStr.toInt();
  }
}

int downloadDICOMRequest()
{
  QString ipPort = "http://47.99.74.239:7000";
  QString loginApi = "/resource/downloadDicom";
  QUrl url(ipPort + loginApi);
  QUrlQuery query;

  query.addQueryItem("puuid", "PA000001");
  query.addQueryItem("token", "P17WG2nb85415Z5eesa455aQ35==");

  url.setQuery(query.query());

  QNetworkRequest request(url);
  request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

  //发送请求
  QNetworkAccessManager manager;
  QNetworkReply* reply = manager.post(request, "");

  QEventLoop eventLoop;
  QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
  eventLoop.exec(QEventLoop::ExcludeUserInputEvents);

  if (reply->error() != QNetworkReply::NoError)
  {
    return reply->error();
  }

  //解析返回的Json结果
  QByteArray replyData = reply->readAll();
  qDebug() << replyData;
  QJsonParseError json_error;
  QJsonDocument jsonDoc(QJsonDocument::fromJson(replyData, &json_error));
  if (json_error.error != QJsonParseError::NoError)
  {
    return -1;
  }
  QJsonObject rootObj = jsonDoc.object();
  QString codeStr = QString("%1").arg(rootObj.value("code").toDouble());
  if (codeStr.compare("200") == 0)
  {
    //返回代码为200的时候证明请求成功对包含的结构数据进行处理
    if (rootObj.contains("result"))
    {
    }
    return 0;
  }
  else
  {
    //请求失败对对应的处理
    return codeStr.toInt();
  }
}

int main(int argc, char* argv[])
{
  QCoreApplication app(argc, argv);
  downloadDICOMRequest();
  app.exec();
  return 0;
}